<div id="sidebar">

<?php

wp_reset_query();
		
if( is_home() || is_page_template('template_blog.php') || is_archive() || is_category() || is_single() ) {

	if ( is_sidebar_active('blog-sidebar') ) {
	
		dynamic_sidebar('blog-sidebar');
	
	}

} else {

	if ( is_sidebar_active('page-sidebar') ) {
    	if ($post->post_parent != 0){
            $thePostID = get_post_ancestors($post);
            $thePostID = end($thePostID);    
        }else{
            $thePostID = $wp_query->post->ID;
        }
        $name_root = get_the_title($thePostID);
        
		//dynamic_sidebar('page-sidebar');
        
        $sibarlf = wp_list_pages('sort_column=menu_order&child_of='.$thePostID.'&title_li=&echo=0');
       
        if(!empty($sibarlf))
        {
            echo '<div class="widget widgetleft"><ul>';
            echo $sibarlf;
            echo '</ul></div>';
        }else
        {
            echo '<div class="widgetleft">';
            dynamic_sidebar('page-sidebar');
            echo '</div>';
        }      
	
	}
	
}

?>

</div><!-- #sidebar (end) -->